home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Comparing Struct elements
- Date: Thu, 01 Feb 1996 16:34:11 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4eqq15$cgt@news.halcyon.com>
- References: <4eo1va$s6k@camelot.ccs.neu.edu>
- NNTP-Posting-Host: blv-pm2-ip25.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- What's happening is you're comapring the address of the
- ClassRec::Grade[] string for the LetGrade instance with the address of
- the string constant "A", "A-", etc. Naturally, the addresses will
- never be the same.
-
- You should write equaility functions, like so
-
- class ClassRec {
- public:
- ...
- BOOL operator==( const char * const lpszGrade)
- { return( 0 == lstrcmpi(Grade, lpszGrade) ); }
- ...
-
- private:
- char Grade[3];
- ...
- };
-
- Then write if( LetGrade == "A") gpa=4.0F; etc.
-
- --Norm
-
-
-
- bhardwaj@ccs.neu.edu (saurabh bhardwaj) wrote:
-
- >Hello,
-
- >I've been trying to compare structure elements using the if Statement. Here
- >is the code:
-
-
- >double NumericVal(ClassRec& LetGrade){
-
- >cout << "assgning struct member usin . " << LetGrade.
- >Grade << '\n';
- >if(LetGrade.Grade == "A") return(4.000);
- >else if(LetGrade.Grade == "A-") return(3.666);
- >else if(LetGrade.Grade == "B+") return(3.333);
- >else return 0;
- >}
-
- >where LetGrade is the following struct:
-
- >struct ClassRec {
- > char Grade[3];
- > int QH;
- > char Name[10];
- > };
-
- >I can't seem to compare the elements of the Grade[3] with "A" or "A-" or "B+"
- >The function invariable returns 0. I've also tried using the -> operator. It
- >doesn't work either!!! Anyone know why??? I'll appreciate any help.
-
- > thanks
- > saurabh
-
-
-